﻿
using CMS.Models;
using CMS.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MahApps.Metro.Controls.Dialogs;

namespace CMS.ViewModel
{
    public class CargoTypeViewModel : ViewModelBase
    {
        private List<CargoType> cargoTypes = new List<CargoType>();
        /// <summary>
        /// 所有物资类型
        /// </summary>
        public List<CargoType> CargoTypes
        {
            get
            {
                return cargoTypes;
            }
            set
            {
                this.cargoTypes = value;
                RaisePropertyChanged();
            }
        }


        public CargoTypeViewModel()
        {
            cargoTypes = new CargoTypeProvider().Select();
        }

        /// <summary>
        /// 打开添加物资类型对话框
        /// </summary>
        public RelayCommand OpenAddGargoTypeWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddCargoTypeWindow();
                    window.ShowDialog();
                    // 刷新数据
                    CargoTypes = new CargoTypeProvider().Select();
                });
            }
        }

        /// <summary>
        /// 删除物资类型命令
        /// </summary>
        public RelayCommand<object> DeleteCargoTypeCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is CargoType model)) return ;
                    var cargos = new CargoProvider().Select().FindAll(t => t.TypeId == model.Id);
                    if (cargos != null && cargos.Count() > 0) { AppData.Instance.MainWindow.ShowMessageAsync("删除失败", "当前类型已有引用"); return; }
                    var count = new CargoTypeProvider().Delete(model);
                    if (count > 0) AppData.Instance.MainWindow.ShowMessageAsync("删除成功",$"成功删除，{model.Name}");
                    // 刷新数据
                    CargoTypes = new CargoTypeProvider().Select();
                });
            }
        }


    }
}
